<template>
  <el-container class="home_container">
    <!-- 顶部 -->
    <el-header>
      <div class="header-left">
        <img class="header-left-img" src="../../assets/imgs/Q版头像.png" alt="" />
        <span class="header-left-txt">后台管理系统</span>
      </div>

      <!-- 右侧用户信息 -->
      <el-dropdown trigger="click" @command="handleCommand">
        <span class="el-dropdown-link">
          <i class="el-icon-user"></i>
          {{ userInfo.name }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>

        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          <el-dropdown-item command="blog">我的博客</el-dropdown-item>
          <el-dropdown-item command="gitee">我的Gitee</el-dropdown-item>
          <el-dropdown-item command="myHome">个人主页</el-dropdown-item>
          <el-dropdown-item command="pic">我的图床</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>

    <el-container>
      <!-- 左侧边栏 -->
      <el-aside width="200px">
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavstate('/' + subItem.path)"
            >
              <template slot="title">
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 中间 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getAllMenus } from '../../api/menus'

export default {
  name: 'home_index',
  data() {
    return {
      userInfo: {
        name: '三百块'
      },
      // 左侧菜单数据
      menulist: [],

      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
    this.userInfo.name = window.sessionStorage.getItem('username')
  },
  methods: {
    // 退出登录
    handleCommand(command) {
      switch (command) {
        case 'logout':
          window.sessionStorage.clear()
          this.$message.success('退出登录成功')
          this.$router.push('/login')
          break

        case 'blog':
          window.open('http://wxinlong.gitee.io')
          break

        case 'gitee':
          window.open('https://gitee.com/wxinlong')
          break

        case 'myHome':
          window.open('https://wxinlong.com/')
          break

        case 'pic':
          window.open('https://tuchuang.wxinlong.com/')
          break

        default:
          break
      }
    },

    // 请求所有菜单
    async getMenuList() {
      const res = await getAllMenus()
      if (res.meta.status === 200) {
        this.menulist = res.data
      } else {
        this.$message.error(res.meta.msg)
      }
    },

    // 保存链接的激活状态
    saveNavstate(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
// 移除菜单栏滚动条
.el-aside::-webkit-scrollbar {
  display: none;
}

.el-aside {
  background-color: #333744;
}

// 解决展开子菜单后多出的 1px
.el-menu {
  border: none;
}

.el-main {
  background-color: #eaedf1;
}

.home_container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;
}
.header-left {
  display: flex;
  align-items: center;
}
.header-left-img {
  height: 40px;
  width: 40px;
  border-radius: 50%;
  border: 1px solid #fff;
}
.header-left-txt {
  font-size: 15px;
  margin-left: 10px;
}

.el-dropdown-link {
  color: #fff;
}
</style>
